<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

$wmlsession = 1;
include("regglobals.php");
include("config.php");
include("lang/".$language);

setlocale (LC_TIME, _DATE_CODE);

// Establish connection with database and select database
$link = mysql_connect($db_host, $db_user, $db_password)
    or die("Could not connect");
mysql_select_db($db_name)
    or die("Could not select database");

// Read date format
$query = "SELECT * FROM wps_dateformat WHERE id = 1";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
$dateformatlong = $row->dateformatlong;
$dateformatmiddle = $row->dateformatmiddle;
$dateformatshort = $row->dateformatshort;

// send wml headers
header("Content-type: text/vnd.wap.wml\n");
echo("<?xml version=\"1.0\" encoding=\""._ENCODING_TAG."\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n");

echo "<wml>\n";

// Choose page

switch($option) {
	case "section" :
		labels();
		require ("wml/sections.php");
		break;
	case "component" :
		labels();
		$query = "SELECT name FROM wps_components WHERE id = '$id'";
		$result = mysql_query($query);
		$row = mysql_fetch_object ($result);
		require ("wml/$row->name.php");
		break;
	case "item" :
		labels();
		require ("wml/items.php");
		break;
	default:
		require ("wml/home.php");
		break;
}

echo "</wml>";

function labels() {
	echo "<template>
<do type=\"prev\" label=\""._BACK."\">
	<prev/>
</do>
<do type=\"options\" label=\""._HOME."\" name=\""._HOME."\">
	<go href=\"index.php\"/>
</do>
</template>\n";
}

function parseUBB($string) {
	$string = preg_replace("/\[b\](.*?)\[\/b\]/si", "<b>\\1</b>", $string);
	$string = preg_replace("/\[i\](.*?)\[\/i\]/si", "<i>\\1</i>", $string);
	$string = preg_replace("/\[strong\](.*?)\[\/strong\]/si", "<strong>\\1</strong>", $string);
	$string = preg_replace("/\[em\](.*?)\[\/em\]/si", "<em>\\1</em>", $string);
	$string = preg_replace("/\[u\](.*?)\[\/u\]/si", "<u>\\1</u>", $string);
	$string = preg_replace("/\[p\](.*?)\[\/p\]/si", "<p>\\1</p>", $string);
	$string = preg_replace("/\[big\](.*?)\[\/big\]/si", "<big>\\1</big>", $string);
	$string = preg_replace("/\[small\](.*?)\[\/small\]/si", "<small>\\1</small>", $string);
	$string = preg_replace("/(^|\s)(http:\/\/\S+)/si", "\\1<a href=\"\\2\">\\2</a>", $string);
	$string = preg_replace("/(^|\s)(www\.\S+)/si", "\\1<a href=\"http://\\2\">\\2</a>", $string);
	$string = preg_replace("/\[url\](http|https|ftp)(:\/\/\S+?)\[\/url\]/si",
		"<a href=\"\\1\\2\">\\1\\2</a>", $string);
	$string = preg_replace("/\[url\](\S+?)\[\/url\]/si",
		"<a href=\"http://\\1\">\\1</a>", $string);
	$string = preg_replace("/\[url=(http|https|ftp)(:\/\/\S+?)\](.*?)\[\/url\]/si",
		"<a href=\"\\1\\2\">\\3</a>", $string);
	$string = preg_replace("/\[url=(\S+?)\](\S+?)\[\/url\]/si",
		"<a href=\"http://\\1\">\\2</a>", $string);
	//$string = preg_replace("/\[email\](\S+?@\S+?\\.\S+?)\[\/email\]/si",
	//	"<a href=\"mailto:\\1\">\\1</a>", $string);
	$string = preg_replace("/\[img\](\S+?)\[\/img\]/si",
		"<img src=\"\\1\" alt=\"image\" />", $string);
	return $string;
}

function split_in_array($string, $length) {
	$i=0;
	while (strlen($string) > 0) {
		$str_array[$i] .= utf_substr($string,  $length);
		$string = substr($string, strlen(utf_substr($string,  $length)));
		$i++;
	}
	return $str_array;
}
function utf_substr($str,$len)
{
    for($i=0;$i<$len;$i++)
    {
        $temp_str=substr($str,0,1);
        if(ord($temp_str) > 127)
        {
            $i++;
            if($i<$len)
            {
                $new_str[]=substr($str,0,3);
                $str=substr($str,3);
            }
        }
        else
        {
            $new_str[]=substr($str,0,1);
            $str=substr($str,1);
        }
    }
    return join($new_str);
} 
